package com.ztgf.pay.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.Result;
import com.ztgf.pay.common.config.YmlConfig;
import com.ztgf.pay.mapper.PayFundDetailsMapper;
import com.ztgf.pay.models.bo.PayZjBalanceLogBO;
import com.ztgf.pay.models.dto.PayFundDetailsDTO;
import com.ztgf.pay.models.service.IPayFundDetailsService;
import com.ztgf.pay.models.service.IPayUserService;
import com.ztgf.pay.models.vo.PayZjBalanceLogVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author ：Eric
 * @description：TODO
 * @date ：2021/9/22 11:31
 */
@DubboService
@Slf4j
public class PayFundDetailsServiceImpl extends ServiceImpl<PayFundDetailsMapper, PayFundDetailsDTO> implements IPayFundDetailsService {

    @DubboReference
    private IPayUserService payUserService;
    @Resource
    private YmlConfig ymlConfig;

    @Override
    @Transactional
    public void saveSync(Date syncDate) {
        boolean isSync = false;
        int pageNo = 1;
        do{
//            log.error("-----syncDate------"+syncDate);
            PayZjBalanceLogBO payZjBalanceLogBO = new PayZjBalanceLogBO();

            payZjBalanceLogBO.setPageNo(pageNo);
            payZjBalanceLogBO.setPageSize(200);

            payZjBalanceLogBO.setStartDate(syncDate);
            payZjBalanceLogBO.setEndDate(syncDate);

            Result<Page<PayZjBalanceLogVO>> pageResult = payUserService.queryPageZjHistoryBalanceLog(payZjBalanceLogBO);
            if (pageResult != null) {
                Page<PayZjBalanceLogVO> data = pageResult.getData();
//                log.error(syncDate + "-----data------"+ JSON.toJSONString(data));
                if (data != null) {
                    if (data.getTotal() > 0) {
                        for (PayZjBalanceLogVO record : data.getRecords()) {
                            save(record);
                        }

                        if(data.getPages() > data.getCurrent()){
                            pageNo = data.getCurrent() + 1;
                            isSync = true;
                        }
                    }
                }
            }
        } while (isSync);


    }

    private void save(PayZjBalanceLogVO record){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("data_no", record.getDataNo());
        PayFundDetailsDTO dto = new PayFundDetailsDTO();
        BeanUtils.copyProperties(record, dto);
        if(!this.update(dto, wrapper)){
            Long id = ymlConfig.getPkId();
            dto.setId(id);
            this.insert(dto);
        }
    }
}
